package com.gitee.elf.api.nms.v1_12_R1

import com.gitee.elf.api.annotation.Id
import com.gitee.elf.api.nms.*
import com.gitee.elf.api.nms.EntityTypeManager
import com.gitee.elf.core.entity.EntityInstance
import net.minecraft.server.v1_12_R1.EntityTypes
import net.minecraft.server.v1_12_R1.MinecraftKey
import org.bukkit.Location
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld
import org.bukkit.entity.Entity
import org.bukkit.entity.EntityType
import org.bukkit.event.entity.CreatureSpawnEvent
import taboolib.common.platform.function.info
import taboolib.library.reflex.Reflex.Companion.invokeConstructor
import taboolib.library.reflex.Reflex.Companion.invokeMethod

@Id("v1_12_R1")
class EntityTypeManager : EntityTypeManager {

    override val types = mutableMapOf<String, Class<NMSEntity>>()

    override fun create(location: Location, instance: Any, type: String): Entity? {

        val entity = types[type.lowercase()]?.invokeConstructor(instance, location) as? net.minecraft.server.v1_12_R1.Entity ?: return null
        entity.setLocation(location.x, location.y, location.z, location.yaw, location.pitch)
        return location.world!!.invokeMethod("addEntity", entity, CreatureSpawnEvent.SpawnReason.CUSTOM)
    }

    @Suppress("UNCHECKED_CAST")
    override fun registerEntity(id: String, bind: EntityType, entity: Class<NMSEntity>) {
        val minecraftKey = MinecraftKey("elf_$id")
        EntityTypes.d.add(minecraftKey)
        EntityTypes.b.a(bind.typeId.toInt(), minecraftKey, entity as Class<out net.minecraft.server.v1_12_R1.Entity>)
    }

}